import string
import urllib.request
from urllib.parse import quote
from bs4 import BeautifulSoup

def nospace(world):
    return world.strip().replace("","").replace("\n","")
city=input("请输入城市：")
code = {
    '北京': '530',
    '上海': '538',
    '广州': '763',
    '深圳': '765',
    '天津': '531',
    '武汉': '736',
    '西安': '854',
    '成都': '801',
    '沈阳': '599',
    '南京': '635',
    '杭州': '653',
    '苏州': '639',
    '重庆': '551',
    '长沙': '749',
    '厦门': '682',
    '南昌': '691'
}
city=code[city]
jobName=input("请输入相关岗位：")
url="https://sou.zhaopin.com/?jl="+city+"&kw="+jobName+"&p=1"
#如果url有中文，用quote转义，不然报错
#url允许使用的字符即键盘上的
url=quote(url,safe=string.printable)
#resp=request.get(url)
#发送http请求
resp=urllib.request.urlopen(url)#获取页面read（）方法来获取响应内容 是二进制格式
#print(resp.content.decode("utf-8"))
content=resp.read().decode("utf-8")
soup=BeautifulSoup(content,"lxml")
#通过beautifulsoup对象去解析页面，同时安装解析器，推荐“lxml”
jobList=soup.select(".joblist-box__item")
job_list=[]
#创建一个list用来存放单个job相关的信息的字典
for job in jobList:
    job_dic={}
    job_name=job.select(".jobinfo__name")[0].get_text()
    #get_txt()用来获取所有文本内容，以字符串列表的方式显示
    #getText() 获取所有文本内容，拼接成一个字符串进行显示
    #print(job_name)#岗位名称
    job_sal=job.select(".jobinfo__salary")[0].getText()
    job_sal=nospace(job_sal)
    if '天' in job_sal:
        job_sal=int(job_sal.split("-")[0])*30
    elif('千' in job_sal):
        job_sal=int(job_sal.split("千")[0])*1000
    elif('万' in job_sal):
        job_sal = float(job_sal.split("万")[0]) * 10000
    else:
        job_sal=5000
    company_name =nospace(job.select(".companyinfo__name")[0].get_text())  # 同时获取 工作地点 工作经验 学历要求job_msg = job.select(".jobinfo__other-info-item")# 工作地点
    job_msg=job.select(".jobinfo__other-info-item")
    job_place = nospace(job_msg[0].getText())
    job_exp = nospace(job_msg[1].getText())
    job_education = nospace(job_msg[2].getText())
    job_dic['job_name']=job_name
    job_dic['job_sal'] = job_sal
    job_dic['company_name'] = company_name
    job_dic['job_place'] = job_place[0:2]
    job_dic['job_exp'] = job_exp
    job_dic['job_education'] = job_education
    print(job_place, job_exp, job_education)
    job_list.append(job_dic)
for i in job_list:
    print(i)